<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;

use think\Db;
use think\facade\Hook;
use app\service\UserService;
use app\service\ResourcesService;
use app\service\RefundLogService;
use app\service\OrderService;
use app\service\MessageService;
use app\plugins\wallet\service\WalletService;

/**
 * 订单售后服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class OrderDetailService
{
    /**
     *买过的商品条件
     */
    public static function OrderDetailWhere($params = [])
    {
         
        // 条件初始化
        $where = [];

        // id
        if(!empty($params['id']))
        {
            $where[] = ['id', '=', intval($params['id'])];
        }
        // 订单号
        if(!empty($params['orderno']))
        {
        	$order_id=Db::name("order")->where(['order_no'=>$params['orderno']])->value("id");
            $where[] = ['order_id', '=', intval($order_id)];

        }
              

        if(!empty($params['keywords']))
        {
            $where[] = ['title', 'like', '%'.$params['keywords'] . '%'];
        }

        
        if(!empty($params['user_id']))
        {
            $where[] = ['user_id', '=', intval($params['user_id'])];
        }   
        
          
        if(!empty($params['upd_time']))
        {
            $where[] = ['upd_time', '>', 0];
        }   
       

        // 时间
        if(!empty($params['time_start']))
        {
            $where[] = ['add_time', '>', strtotime($params['time_start'])];
        }
        if(!empty($params['time_end']))
        {
            $where[] = ['add_time', '<', strtotime($params['time_end'])];
        }

                   
        return $where;
    }

    /*
     *查询数量
     */
    public static function OrderDetailTotal($where = []){
    	 return (int) Db::name('OrderDetail')->where($where)->count();
    }


   
    /**
     * 获取列表    
     * @param   [array]          $params [输入参数]
     */
    public static function OrderDetailList($params = [])
    {
        $where = empty($params['where']) ? [] : $params['where'];
        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;
        $order_by = empty($params['order_by']) ? 'id desc' : $params['order_by'];   
      
        // 获取订单
        $data = Db::name('OrderDetail')->where($where)->limit($m, $n)->order($order_by)->select();
        if(!empty($data))
        {
          
            foreach($data as &$v)
            {
               
                // 创建时间                
                $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);

                // 更新时间
                $v['upd_time'] = date('Y-m-d H:i:s', $v['upd_time']);
             
            }
        }

        return DataReturn('获取成功', 0, $data);
    }

}
?>